package com.hexacta.hrs.quartz.schedulerhelper.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;
import com.hexacta.hrs.quartz.job.PruebaJob;
import com.hexacta.hrs.quartz.schedulerhelper.SchedulerHelperPrueba;

/**
 * 
 * @author nscuri
 * 
 */
public class SchedulerHelperPruebaImpl implements SchedulerHelperPrueba {

    private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy hh:mm:ss");

    private Scheduler scheduler = null;

    public void setScheduler(Scheduler scheduler) {
        this.scheduler = scheduler;
    }

    public Scheduler getScheduler() {
        return scheduler;
    }

    public void setSdf(SimpleDateFormat sdf) {
        this.sdf = sdf;
    }

    public SimpleDateFormat getSdf() {
        return sdf;
    }

    @Override
    public void createJobPrintMessage() {
        try {

            Calendar executionTime = Calendar.getInstance();
            executionTime.add(Calendar.MINUTE, 1);
            String name = this.getSdf().format(executionTime.getTime());
            String group = "PruebaJob";
            JobDetail job = new JobDetail();
            job.setName(name);
            job.setGroup(group);
            job.setJobClass(PruebaJob.class);
            long ctime = System.currentTimeMillis();
            SimpleTrigger trigger = new SimpleTrigger();
            trigger.setJobName(name);
            trigger.setJobGroup(group);
            trigger.setName(name);
            trigger.setGroup(group);
            trigger.setStartTime(new Date(ctime));
            trigger.setRepeatCount(100);
            trigger.setPriority(9);
            trigger.setRepeatInterval(10000);
            trigger.setEndTime(new Date(ctime + 60000L));
            scheduler.scheduleJob(job, trigger);

        } catch (Exception e) {
            throw new RuntimeException("Scheduler error", e);
        }
    }

    @Override
    public void createJobPrintMessageCron() {
        try {
            /**
             * Initiate JobDetail with job name, job group, and executable job
             * class.
             */
            JobDetail jobDetail = new JobDetail("PruebaJobCron3", "PruebaJobCronGroup3", PruebaJob.class);
            /** Initiate CronTrigger with its name and group name. */
            CronTrigger cronTrigger = new CronTrigger("cronTrigger3", "triggerGroup3");
            CronExpression cronExpression = new CronExpression("0 57 18 12 2 ?");
            /** Assign the CronExpression to CronTrigger. */
            cronTrigger.setCronExpression(cronExpression);
            /** schedule a job with JobDetail and Trigger. */
            scheduler.scheduleJob(jobDetail, cronTrigger);

        } catch (Exception e) {
            throw new RuntimeException("Scheduler error", e);
        }
    }

}
